package clue_lou_dao

import (
	"errors"
	"fmt"
	"hft/Database/admin"
	"hft/Database/admin/user_db"
	"hft/Http/Services/new_clue/clue_utils"
	"hft/Http/Services/new_clue_lou/clue_lou_model"
	"hft/e"
	"hft/global"
	"hft/tools"
	"time"
)

// 查找客户
func FindUser(phone string, city int) (data *user_db.User, err error) {
	var user *user_db.User
	if err = global.OC_DB.Where("phone = ? AND city = ?", phone, city).First(&user).Error; err != nil {
		return nil, errors.New(e.UserNotFound)
	} else {
		return user, nil
	}
}

// 创建客户
func CreateUser(city int, ip string, activity clue_lou_model.ActivityClue) (err error) {
	// 找出楼盘
	var lou *admin.LouPan
	var region *admin.Region
	if activity.LouID != 0 {
		if err = global.OC_DB.First(&lou, activity.LouID).Error; err != nil { //找出楼盘ID
			return errors.New(e.LouPanNotExist)
		}
		if err = global.OC_DB.Where("id = ?", lou.Region).First(&region).Error; err != nil {
			return errors.New(e.RegionNotFound)
		}
	} else {
		if err = global.OC_DB.First(&region, city).Error; err != nil {
			return errors.New(e.RegionNotFound)
		}
	}
	var name string
	if activity.LouID == 0 {
		name = ""
	} else {
		name = lou.Name
	}
	// 生成UUID
	var clues []*user_db.Clue
	clues, err = GenerateActivityClue(clues, &activity, ip)
	if err != nil {
		return err
	}
	newPhone := tools.CorrectPhone(activity.Phone)
	release_time := FindCityTimeConfig(city)
	var uuid = tools.GenUUID2()
	var user = map[string]interface{}{
		"create_time":        time.Now(),
		"manager_id":         0,
		"uuid":               uuid,
		"phone":              newPhone,
		"city":               city,
		"intend_region":      int(region.ID),
		"intend_region_name": region.Name,
		"intend_region_link": fmt.Sprintf("%v%v,", region.Link, region.ID),
		"intend_lou":         name,
		"level":              "D",
		"intend_lou_id":      uint(activity.LouID),
		"release_time":       release_time,
		"show":               0,
		"file":               0,
		"tag":                0,
	}
	if err = global.OC_DB.Model(&user_db.User{}).Create(&user).Error; err != nil {
		return errors.New(e.ErrorInfo)
	}
	var newUser *user_db.User
	global.OC_DB.Where("uuid = ?", uuid).First(&newUser)
	global.OC_DB.Model(&newUser).Association("Clues").Append(clues)
	// 添加区域
	global.OC_DB.Model(&user_db.UserRegion{}).Create(map[string]interface{}{
		"user_id":     newUser.ID,
		"region_id":   region.ID,
		"region_link": fmt.Sprintf("%v%v,", region.Link, region.ID),
	})
	return nil
}

// 判断是否在两个时间之间
func isBetween(value, a, b time.Time) bool {
	return value.After(a) && value.Before(b)
}

// 找出当前城市的时间的配置
func FindCityTimeConfig(city int) time.Time {
	// 找出当前的安全时间，确定客户的释放时间
	var citySet *admin.CitySetting
	global.OC_DB.Where("region = ?", city).First(&citySet)
	var startTime = citySet.FreeStartTime
	var endTime = citySet.FreeEndTime
	var s, e = clue_utils.ReturnTimeRange(startTime, endTime)
	var release_time time.Time
	if isBetween(time.Now(), s, e) {
		// 如果在保护期，那么过期时间就在保护期之后一小时
		release_time = e.Add(1 * time.Hour)
	} else {
		release_time = time.Now().Add(1 * time.Hour)
	}
	return release_time
}
